<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-08-22 17:51:39
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\resource\certificateType.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      @submit.native.prevent
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="80px"
    >
      <el-row :gutter="20">
        <el-col :span="5">
          <el-form-item label="仪器类型">
            <el-input
              v-model="queryParams.instrumentTypeName"
              placeholder="请输入"
              clearable
              style="width: 100%"
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-wrapper">
      <el-table
        v-loading="loading"
        :data="dataList"
        height="300px"
      >
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column
          label="仪器类型"
          align="center"
          prop="instrumentTypeName"
          width="400"
        />
        <el-table-column label="证书类型" align="center">
          <template slot-scope="{ row }">
            <span
              @click="viewFile(item.id)"
              class="viewfile"
              v-for="(item, index) in row.certificateTypeList"
              :key="index"
            >
              {{ item.certificateType }}
              <span v-if="index != row.certificateTypeList.length - 1">
                、
              </span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="" align="center" width="10">
          <template>
            <el-button type="text" style="visibility: hidden">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1000px"
      class="equipmentdialog"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rules"
        ref="form"
        size="small"
        label-width="120px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="仪器类型" prop="instrumentTypeId">
              <el-select
                v-model="form.instrumentTypeId"
                placeholder="请选择"
                ref="instrumentTypeId"
                @change="changeType"
                sizi="mini"
              >
                <el-option
                  v-for="item in instrumentTypeSelect"
                  :key="item.key"
                  :label="item.key"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="opration">
        <el-button
          class="btn"
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click.native.prevent="addLine()"
          >添行</el-button
        >
      </div>

      <el-table ref="tableData" :data="form.data" height="400" size="mini">
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="证书类型" align="center" width="300">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.certificateTypeCode"
              placeholder="输入文字检索查询"
              size="mini"
              filterable
              :ref="`selectCer_${scope.$index}`"
              style="width: 100%"
            >
              <el-option
                v-for="item in nameListingSelect"
                :key="item.key"
                :label="item.key"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="校准因子" align="center">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.calibrationFactors"
              placeholder="输入文字检索查询"
              size="mini"
              multiple
              filterable
              clearable
              :ref="`selectFac_${scope.$index}`"
              style="width: 90%"
            >
              <el-option
                v-for="item in calibrationFactorSelectList"
                :key="item.calibrationFactorId"
                :label="item.calibrationFactorName"
                :value="item.calibrationFactorId"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="deleteItem(scope.$index)"
              type="text"
              style="color: #f32d2d"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisibleEdit"
      :close-on-click-modal="false"
      width="900px"
      class="equipmentdialog"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rulesEdit"
        ref="formEdit"
        size="small"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="仪器类型">
              <el-select
                v-model="form.instrumentTypeId"
                :placeholder="status == '3' ? '' : '请选择'"
                ref="instrumentTypeId"
                @change="changeType"
                sizi="mini"
                style="width: 100%"
              >
                <el-option
                  v-for="item in instrumentTypeSelect"
                  :key="item.key"
                  :label="item.key"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证书类型" prop="certificateType">
              <el-select
                v-model="form.certificateTypeCode"
                placeholder="输入文字检索查询"
                size="mini"
                filterable
                style="width: 100%"
              >
                <el-option
                  v-for="item in nameListingSelect"
                  :key="item.key"
                  :label="item.key"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校准因子" prop="calibrationFactors">
              <el-select
                v-model="form.calibrationFactors"
                :placeholder="status == '3' ? '' : '输入文字检索查询'"
                size="mini"
                multiple
                filterable
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="item in calibrationFactorSelectList"
                  :key="item.calibrationFactorId"
                  :label="item.calibrationFactorName"
                  :value="item.calibrationFactorId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" v-if="status != '3'" @click="submitFormEdit"
          >确 定</el-button
        >
        <el-button @click="dialogVisibleEdit = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 校准因子弹框 -->
    <el-dialog
      title="查看校准因子"
      :visible.sync="dialogVisibleFactors"
      :close-on-click-modal="false"
      width="900px"
      v-dialogDrag="true"
    >
      <el-table :data="calibrationFactorsList" height="400px">
        <el-table-column label="序号" type="index" width="100" align="center" />
        <el-table-column label="校准因子" prop="name" align="center" />
      </el-table>

      <div slot="footer">
        <el-button @click="dialogVisibleFactors = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  equipmentCertificateTypeList,
  equipmentCertificateTypeListing,
  equipmentCertificateTypeExport,
  equipmentCertificateTypeDetail,
  equipmentCertificateTypeAdd,
  equipmentCertificateTypeEdit,
  equipmentCertificateTypeRemove,
  nameListing,
  getListByInspectionTypeCode,
  getCalibrationFactors,
} from "@/api/resourse/certificateType";
import { calibrationFactorListing } from "@/api/basicData/calibrationFactor";
import {
  equipmentTypeList,
  instrumentTypeListing,
} from "@/api/resourse/equipmentType";

export default {
  name: "certificateType",
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      rules: {
        instrumentTypeId: [
          { required: true, message: "仪器类型不能为空", trigger: "blur" },
        ],
      },
      rulesEdit: {
        certificateType: [
          { required: true, message: "证书类型不能为空", trigger: "blur" },
        ],
        calibrationFactors: [
          { required: true, message: "校准因子不能为空", trigger: "blur" },
        ],
      },
      // 视图高度
      height: document.documentElement.clientHeight,
      dataList: [],
      form: {},
      status: null,
      title: null,
      loading: false,
      calibrationFactorSelectList: [],
      dialogVisibleEdit: false,
      nameListingSelect: [],
      instrumentTypeSelect: [],
      dialogVisibleFactors: false,
      // 查看校准因子列表
      calibrationFactorsList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true;
      equipmentTypeList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 打开对话框
    openDialog(row, status) {
      this.resetForm("form");
      this.resetForm("formEdit");
      if (row) {
        equipmentCertificateTypeDetail(row.id).then((response) => {
          this.form = response.data;
          this.form.instrumentTypeId =
            this.form.instrumentTypeId && this.form.instrumentTypeId.toString();
          this.form.calibrationFactors = this.form.calibrationFactors
            ? this.form.calibrationFactors.split(",")
            : [];
          this.dialogVisibleEdit = true;
          if (status == "3") {
            this.title = "查看证书类型";
            this.status = "3";
          } else {
            this.title = "编辑证书类型";
            this.status = "2";
          }
        });
      } else {
        this.form = {
          data: [
            {
              calibrationFactors: [],
            },
          ],
        };
        this.status = "1";
        this.title = "添加证书类型";
        this.dialogVisible = true;
      }
    },
    // 添行
    addLine() {
      this.form.data.push({});
      setTimeout(() => {
        console.log(
          "this.$refs",
          this.$refs,
          this.form.data.length - 1,
          `selectCer_${this.form.data.length - 1}`
        );
        this.$refs[`selectCer_${this.form.data.length - 1}`].focus();
      });
      this.$nextTick(() => {
        this.$refs.tableData.bodyWrapper.scrollTop =
          this.$refs.tableData.bodyWrapper.scrollHeight;
      });
    },
    // 删除弹框列表项
    deleteItem(index) {
      this.form.data.splice(index, 1);
    },
    // 更改仪器类型
    changeType(val) {
      this.$nextTick(() => {
        this.form.instrumentTypeName =
          this.$refs.instrumentTypeId.selectedLabel;
      });
    },
    // 提交按钮-添加
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        if (this.form.data.length == 0) {
          this.$modal.msgWarning("请添加证书类型");
          return;
        }
        let emptyIndex = this.form.data.findIndex((item) => {
          return !item.certificateTypeCode;
        });
        if (emptyIndex != -1) {
          this.$modal.msgWarning("请选择证书类型");
          this.$refs[`selectCer_${emptyIndex}`].focus();
          return;
        }

        let emptyIndexS = this.form.data.findIndex((item) => {
          return item.calibrationFactors.length == 0;
        });
        if (emptyIndexS != -1) {
          this.$modal.msgWarning("请选择校准因子");
          this.$refs[`selectFac_${emptyIndexS}`].focus();
          return;
        }
        let form = JSON.parse(JSON.stringify(this.form));
        form.data.forEach((item) => {
          item.calibrationFactors = item.calibrationFactors.join(",");
        });
        if (this.status == "1") {
          equipmentCertificateTypeAdd(form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.dialogVisible = false;
            this.getList();
          });
        } else if (this.status == "2") {
        }
      });
    },
    // 提交按钮-编辑
    submitFormEdit() {
      this.$refs["formEdit"].validate((valid) => {
        if (!valid) return;
        if (this.status == "1") {
        } else if (this.status == "2") {
          let form = JSON.parse(JSON.stringify(this.form));
          form.calibrationFactors = form.calibrationFactors.join(",");
          equipmentCertificateTypeEdit(form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisibleEdit = false;
            this.getList();
          });
        }
      });
    },
    // 查看文件
    viewFile(id) {
      if (id) {
        getCalibrationFactors(id).then((response) => {
          this.calibrationFactorsList = response.data || [];
          this.dialogVisibleFactors = true;
        });
      }
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(
          `是否确认删除${row.instrumentTypeName}的证书类型 [${row.certificateType}] ？`
        )
        .then(() => {
          this.loading = true;
          return equipmentCertificateTypeRemove(row.id);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.tag {
  border: 1px solid #319ed8;
  background: #12b9f64a;
  color: #0892ff;
  padding: 0 5px;
  border-radius: 3px;
  width: fit-content;
  margin: 0 auto;
  margin-bottom: 5px;
  line-height: initial;
  display: inline-block;
}
.imgView {
  color: #2a72ee;
  cursor: pointer;
  margin-right: 10px;
  &:hover {
    text-decoration: underline;
  }
}
.imgcontainer {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  flex-wrap: wrap;
  img {
    width: 45%;
  }
}
::v-deep .star .el-form-item__label::before {
  content: "* ";
  color: red;
}
::v-deep .star {
  display: flex;
  align-items: center;
  .el-form-item__label {
    line-height: inherit;
  }
  .el-form-item__content {
    margin-left: 0 !important;
  }
}
.dialogCer ::v-deep .el-dialog__body .el-form {
  margin-left: -20px;
}
.equipmentdialog ::v-deep .el-dialog__body .el-form {
  margin-left: -35px;
}
.imgitem {
  position: relative;
  height: 20px;
  cursor: pointer;
  .name {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    color: #2a72ee;
    cursor: pointer;
  }
}
.btn {
  position: absolute;
  top: 78px;
  right: 19px;
  padding-inline: 20px;
}
.viewfile {
  color: #2a72ee;
  cursor: pointer;
  display: inline-block;
  width: fit-content;
  &:hover {
    color: #2a72ee;
    text-decoration: underline;
  }
}
</style>
